package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/customers")
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping
    public Result listCustomer(CustomerDto customerDto) {
        log.info("listCustomer: {}", customerDto);
        PageResult<Customer> pageResult = customerService.listCustomer(customerDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody Customer customer) {
        log.info("add: {}", customer);
        customerService.save(customer);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("getById: {}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    @PutMapping
    public Result update(@RequestBody Customer customer) {
        log.info("update: {}", customer);
        customerService.updateById(customer);
        return Result.success();
    }
}
